<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <customBtns :permission="permission" :route-path="routePath" />
      
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="主键" /> -->
        <el-table-column prop="siteName" label="场地" />
        <el-table-column prop="pname" label="所属产品" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="ecode" label="编码" />
        <el-table-column prop="location" label="位置" />
        <el-table-column prop="inuse" label="是否在用" align="center">
          <template slot-scope="scope">
            {{ getDictValue("inuse", scope.row.inuse) }}
          </template>  
        </el-table-column>
        <el-table-column prop="unitname" label="单位" />
        <!-- <el-table-column prop="desci" label="说明" /> -->
        <el-table-column prop="limitlow" label="最小值" />
        <el-table-column prop="limitupper" label="最大值" />
        <el-table-column prop="usedate" label="使用日期" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.usedate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="estatus" label="状态" align="center">
          <template slot-scope="scope">
            {{ getDictValue("estatus", scope.row.estatus) }}
          </template>            
        </el-table-column>
        <el-table-column v-permission="['admin','']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :showEdit="false"
              :customRowBtnObj="customRowBtnObj"
              @customMethod="test"
            />

          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudIotEquip from '@/api/iot/iotEquip'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import customBtns from "@crud/customBtns"
import customOperation from "@crud/customOperation"

const defaultForm = { id: null, siteid: null, pid: null, name: null, ecode: null, location: null, inuse: null, unitname: null, desci: null, limitlow: null, limitupper: null, usedate: null, estatus: null, etype: null, x: null, y: null, z: null, l: null, w: null, h: null, appid: null, rx: null, ry: null, rz: null }
export default {
  name: 'IotEquip',
  components: { pagination, crudOperation, rrOperation, udOperation, customBtns, customOperation, },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '设备', url: 'iot/equip', sort: 'id,desc', crudMethod: { ...crudIotEquip }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'iotEquip:add', ''],
        edit: ['admin', 'iotEquip:edit', ''],
        del: ['admin', 'iotEquip:del', '']
      },
      routePath: {
        add: "/iot/equipSave",
        edit: "/iot/equipSave",
        PK_FIELD: "id"
      },           
      customRowBtnObj: {
        icon: "el-icon-document",
        name: "查看"
      },      
      rules: {
      }    }
  },
  mounted: function() {
  },  
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.$route.query
      if (query && query.siteid) {
        this.crud.query.siteid = query.siteid
      }

      return true
    },
    test(data) {
      const query = {}
      query._PK_ = data.id
      this.$router.push({
        path: "/iot/equipSave",
        query
      })
    }, 
    getDictValue(field, value) {
        let dictItems = [];
        if  (field == "inuse") {
          dictItems = this.crud.extData.dict_yesno
        } else if (field == "estatus") {
          dictItems = this.crud.extData.dict_estatus
        }
        for (let index in dictItems) {
          let item = dictItems[index]
          if (item.k == value) {
            return item.val
          }
        }

    }
  }
}
</script>

<style scoped>

</style>
